package com.hkts.naturalgas.utils;

import lombok.Getter;
import lombok.Setter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @ClassNameCurveUtils
 * @Description 处理排烟架，燃烧架数据曲线 工具类
 * @Author yck
 * @Date2021-06-25 21:04
 * @Version V1.0
 **/
public class CurveUtils {
    @Getter
    private Date startDate;

    SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Getter
    private Date endDate;


    /**
     * 功能描述:
     * 时间列表
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-26 14:48
     * @Description //TODO
     */
    @Getter
    private List<LocalDateTime> dateList = new ArrayList<LocalDateTime>();


    /* 功能描述:
     *增长速度
     */
    @Getter
    @Setter
    private List<Float> growthRate = new ArrayList<Float>();

    /* 功能描述:
     *时间段 每段多长时间
     */
    @Getter
    @Setter
    private List<Integer> timeSlot = new ArrayList<Integer>();

    // 计算结果
    @Getter
    @Setter
    private List<Float> dataResult = new ArrayList<Float>();

    public void setStartDate(String startDate) throws ParseException {
        this.startDate = formate.parse(startDate);
    }

    public void setEndDate(String endDate) throws ParseException {
        this.endDate = formate.parse(endDate);
    }

    //检修之前的增长率
    @Getter
    @Setter
    private List<Float> overhaulGrowthRate = new ArrayList<Float>();
    //检修时间段
    @Getter
    @Setter
    private List<Integer> overhaulTimeSlot = new ArrayList<Integer>();

    /**
     * 功能描述:
     * 通过计算获取数据
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-26 11:06
     * @Description //TODO
     */
    public void getData(Float StartData) {
        Date tempDate = startDate;
        Float tempData = StartData;
        List<Float> dataList = new ArrayList<>();
        while (tempDate.getTime() <= endDate.getTime()) {
            for (int i = 0; i < timeSlot.size(); i++) {
                if (i == 0) {
                    tempData = StartData;
                }
                for (int j = 0; j < timeSlot.get(i); j++) {
                    // 每个单位是一小时 需要转换为分钟或10分钟
                    for (int k = 0; k < 6; k++) {
                        Calendar c = new GregorianCalendar();
                        c.setTime(tempDate);
                        c.add(Calendar.MINUTE, 10);
                        tempDate = c.getTime();
                        System.out.println(tempDate);
                        if (tempDate.getTime() >= endDate.getTime()) {
                            break;
                        }
                        //数据处理
                        tempData = tempData + growthRate.get(i) / 6;
                        dataList.add(tempData);
                        //日期处理
                        dateList.add(tempDate.toInstant()
                                .atZone(ZoneId.systemDefault())
                                .toLocalDateTime());
                        //
                    }
                }
                //处理移动燃烧架需要的时间
                if (timeSlot.size() > 1 && i == timeSlot.size() - 1) {
                    for (int j = 0; j < DataHandlerUtils.getRundom(); j++) {
                        Calendar c = new GregorianCalendar();
                        c.setTime(tempDate);
                        c.add(Calendar.MINUTE, 10);
                        tempDate = c.getTime();
                        System.out.println(tempDate);
                        if (tempDate.getTime() >= endDate.getTime()) {
                            break;
                        }
                        //数据处理
                        dataList.add(0f);
                        //日期处理
                        dateList.add(tempDate.toInstant()
                                .atZone(ZoneId.systemDefault())
                                .toLocalDateTime());
                    }
                }
            }
        }
        dataResult.addAll(dataList);
    }

    /**
     * 功能描述:
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-28 15:59
     * @Description //TODO
     * 处理检修交接时段的数据
     */
    public void getOverhaulData(Float StartData, Integer minutes) {
        Date tempDate = startDate;
        Float currentValue = StartData;
        //当前时间所用的比例
        Float currentProportion = 0f;
        int recordTime=0;
        //计算起始位置的值
        for (int i = 0; i < overhaulTimeSlot.size(); i++) {
            for (int j = 0; j < overhaulTimeSlot.get(i); j++) {
                for (int k = 0; k < 6; k++) {
                    recordTime=recordTime+10;
                    if(recordTime>minutes){
                        break;
                    }else{
                        currentValue = currentValue + overhaulGrowthRate.get(i) / 6;
                    }
                }
            }
        }
        //计算当前时间点所在那个周期那个点
        int currentNode = 0;
        int totalMinute=0;
        for (int i = 0; i < overhaulTimeSlot.size(); i++) {
             totalMinute = overhaulTimeSlot.get(i) * 60;
            if (minutes > totalMinute) {
                minutes = minutes - totalMinute;

            } else {
                currentNode = i;
                currentProportion = (float) minutes / totalMinute;
                break;
            }
        }
        /**
         * 处理第一周期
         */
        int minuterRecord = 0;
        for (int i = currentNode; i < timeSlot.size(); i++) {

            for (int j = 0; j < timeSlot.get(i); j++) {
                for (int k = 0; k < 6; k++) {
                    //先判断是否到所占百分比
                    if (i == currentNode) {
                        minuterRecord = minuterRecord + 10;
                      float a= (float) minuterRecord / (timeSlot.get(i) * 60);
                        if (a<= currentProportion) {
                            continue;
                        }else{
                            Calendar c = new GregorianCalendar();
                            c.setTime(tempDate);
                            c.add(Calendar.MINUTE, 10);

                            tempDate = c.getTime();
                            currentValue = currentValue + growthRate.get(i) / 6;

                            if (tempDate.getTime() >= endDate.getTime()) {
                                break;
                            }
                            //数据处理
                            dataResult.add(currentValue);
                            //日期处理
                            dateList.add(tempDate.toInstant()
                                    .atZone(ZoneId.systemDefault())
                                    .toLocalDateTime());
                        }
                    }else{
                        Calendar c = new GregorianCalendar();
                        c.setTime(tempDate);
                        c.add(Calendar.MINUTE, 10);

                        tempDate = c.getTime();
                        currentValue = currentValue + growthRate.get(i) / 6;

                        if (tempDate.getTime() >= endDate.getTime()) {
                            break;
                        }
                        //数据处理
                        dataResult.add(currentValue);
                        //日期处理
                        dateList.add(tempDate.toInstant()
                                .atZone(ZoneId.systemDefault())
                                .toLocalDateTime());
                    }
                }
            }
        }
        //移动燃烧架
        if(tempDate.getTime()<= endDate.getTime()){
            for (int j = 0; j < DataHandlerUtils.getRundom(); j++) {
                Calendar c = new GregorianCalendar();
                c.setTime(tempDate);
                c.add(Calendar.MINUTE, 10);
                tempDate = c.getTime();
                System.out.println(tempDate);
                //数据处理
                dataResult.add(0f);
                //日期处理
                dateList.add(tempDate.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime());
            }
            startDate = tempDate;
            //处理后续数据
            getData(StartData);
        }

    }
}
